//
//  ThemeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/31.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol ThemeViewDelegate {
    @objc optional func selectCardType(title:String) //选择卡片类型
}

class ThemeView: UIView {
    
    weak var themeDelegate : ThemeViewDelegate?
    
    fileprivate let cellId = "themeCell"
    
    fileprivate let dataArray = [shoppingCard,studentCard,sportCard,businessCard,standardCard,ownerCard]
    fileprivate let images = [#imageLiteral(resourceName: "gwk"),#imageLiteral(resourceName: "xsk"),#imageLiteral(resourceName: "ydk"),#imageLiteral(resourceName: "slk"),#imageLiteral(resourceName: "bzk"),#imageLiteral(resourceName: "czk")]
    
    fileprivate let moveView = UIView()
    
    let moreButton = UIButton()
    
    let arrowButton = UIButton()

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.mineColor()
        
        let headView = UIView()
        headView.backgroundColor = UIColor.white
        self.addSubview(headView)
        
        let logoImageView = UIImageView()
        logoImageView.image = #imageLiteral(resourceName: "rmzt")
        headView.addSubview(logoImageView)
        
        let titleLabel = UILabel()
        titleLabel.text = hotTopic
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        headView.addSubview(titleLabel)
        
        moreButton.setTitle(moreTopic, for: .normal)
        moreButton.setTitleColor(UIColor.black, for: .normal)
        moreButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        headView.addSubview(moreButton)
        
        arrowButton.layer.contents = #imageLiteral(resourceName: "next").cgImage
        headView.addSubview(arrowButton)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize.init(width: ScreenW/4, height: RATIO_H(maxNum: 100))
        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.bounces = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(BankCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        moveView.backgroundColor = UIColor.moveColor()
        self.addSubview(moveView)
        
        headView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        logoImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 8))
            make.centerY.equalToSuperview()
            make.width.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(logoImageView.snp.right).offset(RATIO(maxNum: 8))
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        arrowButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -8))
            make.centerY.equalToSuperview()
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        
        moreButton.snp.makeConstraints { (make) in
            make.right.equalTo(arrowButton.snp.left)
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalTo(headView.snp.bottom).offset(RATIO_H(maxNum: 2))
            make.height.equalTo(RATIO_H(maxNum: 100))
        }
        
        moveView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 5))
            make.width.equalTo(ScreenW/4 * 3)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ThemeView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? BankCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        cell?.headImageView.image = images[indexPath.row]
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.themeDelegate?.selectCardType!(title: dataArray[indexPath.row])
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        moveView.frame.origin.x = scrollView.contentOffset.x/2
    }
    
}
